Appearance
1、服务调用
服务注册、服务发现、服务调用
在Spring Cloud Alibaba生态系统中,服务调用的核心组件是 Nacos Discovery 和 Feign。
Nacos Discovery
Nacos是一个开源的服务发现和配置管理平台,它提供了服务注册与发现的功能,可以让微服务应用在注册到Nacos注册中心后,方便地发现其他服务,并实现服务之间的调用。通过Nacos Discovery,服务之间的关系和位置信息能够得到维护,实现了服务注册和服务发现。
Feign
Feign是一个声明式的Web服务客户端,它简化了服务调用的过程。通过在接口上添加注解,Feign能够自动构造HTTP请求并将其发送到服务提供方。在Spring Cloud Alibaba中,Feign与Nacos Discovery集成,可以使用Feign来发起服务调用,而Nacos Discovery则用于服务的注册和发现。
微服务间的调用方式
微服务调用
微服务之间的通信方式常见的方式有两种:
RPC 代表:dubbo
HTTP 代表:SpringCloud在微服务架构中,最常见的场景就是微服务间的相互调用。
在 SpringCloud 中,默认是使用 HTTP 来进行微服务的通信,微服务间的相互调用方式主要有RestTemplate、Feign 、和OpenFeign
RestTemplate
RestTemplate 是从 Spring3.0 开始支持的一个 http 请求工具,这个请求工具是 Spring 自带的
RestTemplate 提供了常见的 REST 请求方法模板,如 GET、POST、PUT、DELETE 请求以及一些通用的请求执行方法 exchange 和 execute 方法
Feign
Feign 是 Spring Cloud 组件中的一个声明式的轻量级 RESTful 的 HTTP 服务客户端
Feign 内置了 Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务;
OpenFeign
OpenFeign 是 Spring Cloud 在 Feign 的基础上支持 SpringMVC 的注解,如 @RequesMapping 等等。
OpenFeign 的 @FeignClient 可以解析 SpringMVC 的 @RequestMapping 注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务
OpenFeign 和 Feign 组件的区别和关联
OpenFeign 是 Feign 的增强版,它是 Spring Cloud 对 Feign 进行了扩展和优化的结果。虽然两者有关联,但在一些功能和使用上存在一些区别。
Feign:
- Feign 是一个声明式的Web服务客户端,用于简化服务调用的过程。
- 它基于接口定义,通过添加注解,可以轻松地发起HTTP请求。
- 在原始的 Spring Cloud 中,Feign 的默认实现不支持 Spring MVC 注解,因此使用起来有一些限制。
OpenFeign:
- OpenFeign 是对 Feign 的增强,它引入了对 Spring MVC 注解的支持,使得 Feign 在使用上更加灵活。
- OpenFeign 提供了更丰富的功能,例如继承性、支持 Spring Cloud Contract、支持多部分请求和响应等。
- OpenFeign 可以通过
@FeignClient注解的contextId属性来实现多个 Feign 客户端实例之间的隔离。
在Spring Cloud项目中,如果你使用 @FeignClient 注解,实际上默认是使用的 OpenFeign。
Spring Cloud 在 Hoxton.RELEASE 版本之后默认使用 OpenFeign,而不是原始的 Feign。
关于使用 @FeignClient 注解创建 Feign 客户端的例子:
@FeignClient(name = "example-service", url = "http://example.com")
public interface ExampleFeignClient {
@GetMapping("/hello")
String hello();
}在上述例子中,@FeignClient 注解指定了服务的名称和URL,ExampleFeignClient 接口定义了服务调用的契约。通过添加 @GetMapping 注解,实现了对服务提供者的 /hello 接口的调用。
总体而言,OpenFeign 是对 Feign 的增强版本,提供了更多的功能和更好的灵活性,而在 Spring Cloud 中,我们常常通过 @FeignClient 注解来使用 OpenFeign。
2、OpenFeign 的使用
@FeignClient的基本使用方法
@FeignClient 是 Spring Cloud 中用于声明式REST客户端的注解。通过使用该注解,可以定义一个接口,而不需要实现该接口,Spring Cloud会在运行时自动生成该接口的实现类,用于发起服务间的HTTP请求。
以下是@FeignClient的基本使用方法:
简单使用:
在一个接口上添加 @FeignClient 注解,并指定要调用的服务名称
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "example-service")
public interface ExampleFeignClient {
@GetMapping("/hello")
String hello();
}在上述例子中,ExampleFeignClient 接口用于调用名为 "example-service" 的微服务,其中 @GetMapping("/hello") 定义了调用服务的路径。
指定 URL
可以通过 url 属性指定服务的URL,而不是通过服务名称进行调用。
@FeignClient(name = "example-service", url = "http://example.com")
public interface ExampleFeignClient {
@GetMapping("/hello")
String hello();
}在这个例子中,服务的URL被硬编码为 "http://example.com"。
指定Fallback
可以通过 fallback 属性指定一个实现了 ExampleFeignClient 接口的类,作为调用失败时的降级处理。
@FeignClient(name = "example-service", fallback = ExampleFeignClientFallback.class)
public interface ExampleFeignClient {
@GetMapping("/hello")
String hello();
}
public class ExampleFeignClientFallback implements ExampleFeignClient {
@Override
public String hello() {
return "Fallback Hello";
}
}在这个例子中,当调用服务失败时,会执行 ExampleFeignClientFallback 中的逻辑。
指定Decoder和Encoder
可以通过 decoder 和 encoder 属性指定自定义的解码器和编码器。
@FeignClient(name = "example-service", decoder = MyDecoder.class, encoder = MyEncoder.class)
public interface ExampleFeignClient {
@GetMapping("/hello")
String hello();
}在这个例子中,MyDecoder 和 MyEncoder 分别是自定义的解码器和编码器。
这些是 @FeignClient 注解的基本用法,通过灵活使用这些属性,可以实现更多定制化的服务调用配置。
to be continued....
参考